package com.vnp.vchat.adapter;

import java.util.ArrayList;

import se.emilsjolander.stickylistheaders.StickyListHeadersAdapter;

import android.content.Context;
import android.graphics.Bitmap.Config;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.vnp.vchat.R;
import com.vnp.vchat.UIControls.MyCircleImageView;
import com.vnp.vchat.bean.ObjContact;

public class ContactAdapter extends ArrayAdapter<ObjContact> implements StickyListHeadersAdapter{

	Context context;
	ArrayList<ObjContact> ObjContacts = new ArrayList<ObjContact>();
	DisplayImageOptions options = new DisplayImageOptions.Builder()
						.cacheInMemory(false)
						.cacheOnDisc(true)
						.showImageForEmptyUri(R.drawable.defaul_avatar)
						.showImageOnFail(R.drawable.defaul_avatar)
						.bitmapConfig(Config.RGB_565)
						.displayer(new FadeInBitmapDisplayer(300))
						.build();
	
	private LayoutInflater inflater;

	public ContactAdapter(Context context, ArrayList<ObjContact> ObjContacts) {
		super(context, R.layout.item_search_result, ObjContacts);
		this.context = context;
		this.ObjContacts = ObjContacts;
		inflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return ObjContacts.size();
	}

	@Override
	public ObjContact getItem(int position) {
		return ObjContacts.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}


	@Override
	public View getView(int position, View v, ViewGroup parent) {
		Holder holder = null;
		if (v==null){
			v = inflater.inflate(R.layout.item_contact, parent, false);
			holder = new Holder();
			
			holder.imgAvatar = (MyCircleImageView) v.findViewById(R.id.img_avatar);
			holder.txtUseName = (TextView) v.findViewById(R.id.use_name);
			holder.txtLoginName = (TextView) v.findViewById(R.id.login_name);
			
			holder.imgAvatar.setVisibility(View.GONE);
			
			v.setTag(holder);
		} else {
			holder = (Holder) v.getTag();
		}
		
		holder.txtUseName.setText(ObjContacts.get(position).getName());
		holder.txtLoginName.setText(ObjContacts.get(position).getPhone());
		
		return v;
	}
	
	class Holder{
		MyCircleImageView imgAvatar;
		TextView txtUseName;
		TextView txtLoginName;
	}
	
	class HeaderViewHolder {
        TextView text;
    }

	@Override
	public View getHeaderView(int position, View convertView, ViewGroup parent) {
		 HeaderViewHolder holder;
	        if (convertView == null) {
	            holder = new HeaderViewHolder();
	            convertView = inflater.inflate(R.layout.header_listcontact, parent, false);
	            holder.text = (TextView) convertView.findViewById(R.id.text);
	            convertView.setTag(holder);
	        } else {
	            holder = (HeaderViewHolder) convertView.getTag();
	        }
	        //set header text as first char in name
	        String headerText = "" + ObjContacts.get(position).getName().subSequence(0, 1).charAt(0);
	        holder.text.setText(headerText);
	        return convertView;
	}

	@Override
	public long getHeaderId(int position) {
		return ObjContacts.get(position).getName().subSequence(0, 1).charAt(0);
	}
	
	

}
